﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Model;

namespace CheckListBusiness
{
    public class Workspace
    {
        public Workspace(){
            _checkListProject.IList = GetCheckListItemList(ConfigurationManager.AppSettings["本质安全审查"], "InherentSafety");
            _checkListProject.FList = GetCheckListItemList(ConfigurationManager.AppSettings["设施布置审查"], "FacilitySitingChecklist");
            _checkListProject.PList = GetCheckListItemList(ConfigurationManager.AppSettings["人为因素审查"], "HumanFactors");
            _iDefaultCount = _checkListProject.IList.Count;
            _fDefaultCount = _checkListProject.FList.Count;
            _pDefaultCount = _checkListProject.PList.Count;
        }

        public int IDefaultCount{
            get { return _iDefaultCount; }
            set { _iDefaultCount = value; }
        }

        public int FDefaultCount{
            get { return _fDefaultCount; }
            set { _fDefaultCount = value; }
        }

        public int PDefaultCount{
            get { return _pDefaultCount; }
            set { _pDefaultCount = value; }
        }

        private int _iDefaultCount;
        private int _fDefaultCount;
        private int _pDefaultCount;

        private CheckListProject _checkListProject = CheckListProject.CheckListProjectInstance;

        public CheckListProject CheckListProject{
            get { return _checkListProject; }
            set { _checkListProject = value; }
        }

        private static List<string> _hideColumns = new List<string>();
        private readonly static List<string> _allColumns = new List<string>() { "序号", "检查项", "危害", "建议项类别", "建议项编号", "建议项"
        ,"负责人","备注"};
        public static List<string> AllColumns
        {
            get { return _allColumns; }
        }

        public List<string> GetHideColumns()
        {
            return _hideColumns;
        }

        public List<string> GetAllColumns()
        {
            return _allColumns;
        }

        public event Action<List<string>> ColumnsChanged;

        public void OnColumnsChanged(List<string> s)
        {
            if (ColumnsChanged != null)
            {
                ColumnsChanged(s);
            }
        }

        public void HideColumns(List<string> cols)
        {
            _hideColumns.Clear();
            foreach (string s in _allColumns)
            {
                if (!cols.Contains(s))
                {
                    _hideColumns.Add(s);
                }
            }
            OnColumnsChanged(_hideColumns);
        }

        public void InitProjectInstance(string comName, string proName, string proLoc, string TecUni, string type)
        {
            NewProjectDetails projectDetails = new NewProjectDetails(comName, proName, proLoc, TecUni, type);
            _checkListProject.ProjectDetails = projectDetails;
        }

        public void SaveProject(string fileName)
        {
            BinaryFormatter binaryFormtter = new BinaryFormatter();
            Stream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
            binaryFormtter.Serialize(fstream, _checkListProject);
            fstream.Close();
        }

        public CheckListProject OpenProject(string fileName)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            Stream fstream = File.OpenRead(fileName);
            _checkListProject = (CheckListProject)binaryFormatter.Deserialize(fstream);
            fstream.Close();
            return _checkListProject;
        }

        public List<string> GetPidNumList()
        {
            List<string> res = new List<string>();
            foreach (PID pid in _checkListProject.PidList)
            {
                res.Add(pid.DrawingNum);
            }
            return res;
        }

        public List<string> GetPsiList()
        {
            return _checkListProject.PsiList;
        }

        public void SavePsiList(List<string> list)
        {
            _checkListProject.PsiList.Clear();
            _checkListProject.PsiList = list;
        }

        public static List<CheckListItem> GetCheckListItemList(string fileName, string sheetName){
            return DataAccess.CSVHelper.GetCheckListItems(fileName, sheetName);
        }

        private static List<string> _titleList = new List<string>();
        public List<string> TitleList
        {
            get { return _titleList; }
            set { _titleList = value; }
        }

        static Workspace(){
            _titleList = DataAccess.CSVHelper.GetTitleList();
        }

        public int GetAdviceCount(string name){
            int count = 0;
            switch (name){
                case"本质安全审查":
                    foreach (CheckListItem item in _checkListProject.IList)
                    {
                        if (!string.IsNullOrEmpty(item.Advice))
                            count++;
                    }
                    break;
                case "人为因素审查":
                    foreach (CheckListItem item in _checkListProject.PList)
                    {
                        if (!string.IsNullOrEmpty(item.Advice))
                            count++;
                    }
                    break;
                case "设施布置审查":
                    foreach (CheckListItem item in _checkListProject.FList)
                    {
                        if (!string.IsNullOrEmpty(item.Advice))
                            count++;
                    }
                    break;
                default:break;
            }
            return count;
        }
    }
}
